package com.echoesnet.echoesshortvideo.Utils.Input;

import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

import com.echoesnet.echoesshortvideo.Utils.StringUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;

import java.io.UnsupportedEncodingException;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/27 20:25
 * @description
 */
public class InputWatcher implements TextWatcher
{
    private int maxLen = 0;
    private EditText editText = null;

    public InputWatcher(int maxLen, EditText editText)
    {
        this.maxLen = maxLen;
        this.editText = editText;
    }

    public void afterTextChanged(Editable arg0)
    {

    }

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3)
    {

    }

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
    {
        try
        {
            Editable editable = editText.getText();
            String content = editText.getText().toString().trim();
            byte[] bt = content.getBytes("GB18030");//在此编码格式下，一个汉字算两个英文
            if (bt.length > maxLen)
            {
                ToastUtils.showShort("输入字符数超过限制");
                int selEndIndex = Selection.getSelectionEnd(editable);
                String str = editable.toString();
                byte[] bt2 = str.getBytes("GB18030");
                byte[] bt3 = subBytes(bt2, 0, maxLen);
                String newStr = gbToString(bt3);
                String temp = String.valueOf(newStr.charAt(newStr.length() - 1));
                boolean flag = StringUtils.isContainChinese(temp);
                boolean flag2 = StringUtils.isContainLetters(temp);
                boolean flag3 = StringUtils.isContainNumber(temp);
                if (!flag && !flag2 && !flag3)
                {
                    String newStr2 = newStr.substring(0, newStr.length() - 1);
                    editText.setText(newStr2);
                }
                else
                {
                    editText.setText(newStr);
                }
                editable = editText.getText();
                // 新字符串的长度
                int newLen = editable.length();
                // 旧光标位置超过字符串长度
                if (selEndIndex >= newLen)
                {
                    selEndIndex = editable.length();
                }
                //设置新光标所在的位置
                Selection.setSelection(editable, selEndIndex);
            }

        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

    }

    private byte[] subBytes(byte[] src, int begin, int count)
    {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }

    private String gbToString(byte[] data)
    {
        String str = null;
        try
        {
            str = new String(data, "gb2312");
        } catch (UnsupportedEncodingException e)
        {
        }
        return str;
    }
}
